package com.china08.yxyapi.api.manage.tec.teaching.lesson.topic;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsVideo;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsVideoRepository;
import com.china08.yxyapi.service.db.YxyKeChengService;
import com.china08.yxyapi.util.StringTrimUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsTopicCrtItemController extends YxyApiBaseController {

    @Autowired
    YxyLessonsTopicLessonItemRepository itemRepository;
    @Autowired
    YxyLessonsTopicLessonRepository lessonRepository;

    @Autowired
    YxyLessonsVideoRepository videoRepository;

    @Autowired
    YxyKeChengService yxyKeChengServiceImpl;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/topic/item/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String lessonId = reqModel.lessonId;
        String chapterId = reqModel.chapterId;
        String title = StringTrimUtil.trim(reqModel.title);
        String intro = reqModel.intro;
        String videoId = reqModel.videoId;

        YxyLessonsTopicLessonItem item = new YxyLessonsTopicLessonItem();
        item.setId(UUID.randomUUID().toString().replace("-", ""));
        item.setLessonId(lessonId);
        item.setChapterId(chapterId);
        item.setTitle(title);
        item.setIntro(intro);
        item.setStatus(0);
        item.setPreview(0);
        item.setVideoId(videoId);
        itemRepository.save(item);

        // 获取视频时常
        Integer duration = 0;
        YxyLessonsVideo video = videoRepository.findById(videoId);
        if (null != video) {
            duration = video.getDuration();
        }

        // 修改课程课时数量
        YxyLessonsTopicLesson lesson = lessonRepository.findById(lessonId);
        if (null != lesson) {
            lesson.setSync(0);
            lesson.setCount(lesson.getCount() + 1);
            lesson.setIsVisible(0);
            lesson.setDownType("其它");
            lesson.setDuration(lesson.getDuration() + duration);
            lessonRepository.save(lesson);
        }

        // 每次添加课时修改课时的排序
        List<Map<String, Object>> itemList = yxyKeChengServiceImpl.findTopicItemList(lessonId);

        Integer orderNum = 1;
        for (Map<String, Object> map : itemList) {
            String itemId = map.get("id") + "";
            YxyLessonsTopicLessonItem it = itemRepository.findById(itemId);
            it.setOrderNum(orderNum);
            itemRepository.save(it);
            orderNum++;
        }
        return null;
    }

    public static class ReqModel extends YxyApiReqModel {
        public String lessonId;
        public String chapterId;
        public String title;
        public String intro;
        public String videoId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(lessonId), "课程不能为空");
            Assert.isTrue(StringUtils.isNotBlank(chapterId), "章节不能为空");
            Assert.isTrue(StringUtils.isNotBlank(title), "课时名称不能为空");
            Assert.isTrue(StringUtils.isNotBlank(videoId), "视频不能为空");
            Assert.isTrue(title.length() > 0 && title.length() < 36, "请输入1-35个字的视频名称");
            if (StringUtils.isNotBlank(intro)) {
                Assert.isTrue(intro.length() > 0 && intro.length() < 501, "请输入1-500个字的课时简介");
            }
        }
    }

    public static class RespModel extends YxyApiRespModel {
    }
}
